package com.cgly.medical.service.impl;


import com.alibaba.excel.EasyExcel;
import com.cgly.medical.domain.Account;
import com.cgly.medical.domain.Doctor;
import com.cgly.medical.excel.DoctorData;
import com.cgly.medical.listener.DoctorExcelListener;
import com.cgly.medical.mapper.DoctorMapper;
import com.cgly.medical.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wangxiangli
 * @create 2022-08-16-16:08
 */
@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public List<Doctor> selectAll(Doctor doctor) {
        List<Doctor> doctors = doctorMapper.selectAll(doctor);
        return doctors;
    }


    @Override
    public Doctor selectDoctorByid(String id) {
        Doctor doctor = doctorMapper.selectDoctorByid(id);
        return doctor;
    }

    @Override
    public int updateDoctorInfo(Doctor doctor) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
        doctor.setUpdatetime(dateFormat.format(date));
        int i = doctorMapper.updateDoctorInfo(doctor);
        return i;
    }

    @Override
    public int deleteDoctorinfo(String id) {
        int i=doctorMapper.deleteDoctorinfo(id);
        return i;
    }

    @Override
    public int addDoctorinfo(Doctor doctor) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//定义格式
        doctor.setCreatetime(dateFormat.format(date));
        doctor.setUpdatetime(dateFormat.format(date));
        int i=doctorMapper.addDoctorinfo(doctor);
        return i;
    }

    @Override
    public Account selectAccountByPhone(String phone) {
        Account account=doctorMapper.selectAccountByPhone(phone);
        return account;
    }

    @Override
    public List<Doctor> selectByTypeId(Integer typeId) {
        List<Doctor> doctorList = doctorMapper.selectByTypeId(typeId);
        return doctorList;
    }

    @Override
    public void BatchaddDoctors(MultipartFile file, DoctorService doctorService) {
        try{
            //文件输入流
            InputStream in=file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, DoctorData.class,new DoctorExcelListener(doctorService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Doctor selectDoctorByPhone(String phone) {
        Doctor doctor = doctorMapper.selectDoctorByPhone(phone);
        return doctor;
    }

    @Override
    public Long getDoctorId(String username) {
        return doctorMapper.getDoctorId(username);
    }

}
